package com.cloudkinto.service.operate.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.utils.StringsUtil;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.dao.OperateRecordDao;
import com.cloudkinto.dao.UserDao;
import com.cloudkinto.entity.OperateRecordDo;
import com.cloudkinto.entity.UserDo;
import com.cloudkinto.enumType.operate.OperateTypeEnum;
import com.cloudkinto.service.operate.OperateRecordService;
import com.cloudkinto.vo.operate.OperateRecordVo;
import com.cloudkinto.vo.operate.OperateRes;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author zhangyy
 * @since 2020-07-09
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class OperateRecordServiceImpl extends ServiceImpl<OperateRecordDao, OperateRecordDo> implements OperateRecordService {
    @Autowired
    private OperateRecordDao dao;
    @Autowired
    private UserDao userDao;

    @Override
    public PageResult getStockToPlatformRecord(Map<String, Object> map) {
        map.put("type", OperateTypeEnum.PlatProduct_StockTO_Platform.getValue());
        IPage<OperateRecordVo> page = pageInit(map);

        List<OperateRecordVo> stockToPlatformRecord = dao.getStockToPlatformRecord(page, map);
        List<OperateRes> operateResList = new ArrayList<>();
        for (OperateRecordVo operateRecordDo : stockToPlatformRecord) {
            OperateRes operateRes = new OperateRes();
            operateRes.setOperateTime(operateRecordDo.getOperateTime());
            String i18Message;
            if (StringUtils.isNotBlank(operateRecordDo.getContentValue())) {
                if (operateRecordDo.getContentValue().contains(SysConstant.operateRecordSeparator)) {
                    String[] values = operateRecordDo.getContentValue().split(SysConstant.operateRecordSeparator);
                    for (int i = 0; i < values.length; i++) {
                        String value = values[i];
                        values[i] = StringsUtil.createI18Message(value);
                    }
                    i18Message = StringsUtil.createI18Message(operateRecordDo.getContent(), values);
                } else {
                    String value = StringsUtil.createI18Message(operateRecordDo.getContentValue());
                    i18Message = StringsUtil.createI18Message(operateRecordDo.getContent(), value);
                }
            } else {
                i18Message = StringsUtil.createI18Message(operateRecordDo.getContent());
            }
            operateRes.setContent(i18Message);
            operateRes.setOperateBy(operateRecordDo.getOperateUsername());
            operateResList.add(operateRes);
        }
        PageResult result = new PageResult(operateResList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    private IPage<OperateRecordVo> pageInit(Map<String, Object> map) {
        IPage<OperateRecordVo> page = new Page<>();
        page.setSize(map.get("pageSize") != null ? Long.parseLong(map.get("pageSize").toString()) : 10);
        page.setCurrent(map.get("current") != null ? Long.parseLong(map.get("current").toString()) : 1);
        return page;
    }

    /**
     * 1:CM订单 2:CM商品 3:CM库存 4:库存导入操作
     * StaticDict.Operate_Type
     *
     * @param thirdId
     * @param type
     * @return
     */
    @Override
    public List<OperateRes> getOperateList(Long thirdId, OperateTypeEnum type) {
        List<OperateRes> operateResList = new ArrayList<>();
        List<OperateRecordVo> list = dao.list(thirdId, type.getValue());
        for (OperateRecordVo operateRecordDo : list) {
            OperateRes operateRes = new OperateRes();
            operateRes.setOperateTime(operateRecordDo.getOperateTime());
            String i18Message;
            if (StringUtils.isNotBlank(operateRecordDo.getContentValue())) {
                if (operateRecordDo.getContentValue().contains(SysConstant.operateRecordSeparator)) {
                    String[] values = operateRecordDo.getContentValue().split(SysConstant.operateRecordSeparator);
                    for (int i = 0; i < values.length; i++) {
                        String value = values[i];
                        values[i] = StringsUtil.createI18Message(value);
                    }
                    i18Message = StringsUtil.createI18Message(operateRecordDo.getContent(), values);
                } else {
                    String value = StringsUtil.createI18Message(operateRecordDo.getContentValue());
                    i18Message = StringsUtil.createI18Message(operateRecordDo.getContent(), value);
                }
            } else {
                i18Message = StringsUtil.createI18Message(operateRecordDo.getContent());
            }
            operateRes.setContent(i18Message);
            operateRes.setOperateBy(operateRecordDo.getOperateUsername());
            operateResList.add(operateRes);
        }

        return operateResList;
    }

    @Override
    public boolean operateAddRecord(String content, Long thirdId, Long userId, OperateTypeEnum type) {
        OperateRecordDo operateRecordDo = new OperateRecordDo();
        operateRecordDo.setContent(content);
        operateRecordDo.setOperateTime(new Date());
        operateRecordDo.setOperateBy(userId);
        operateRecordDo.setType(type.getValue());
        operateRecordDo.setThirdId(thirdId);
        operateRecordDo.setDeleteFlag(0);
        dao.insert(operateRecordDo);
        return true;
    }

    @Override
    public boolean operateAddRecord(String content, List<String> thirdIds, Long userId, OperateTypeEnum type) {
        if (thirdIds != null && thirdIds.size() > 0) {
            for (String thirdId : thirdIds) {
                operateAddRecord(content, Long.parseLong(thirdId), userId, type);
            }
        }
        return true;
    }

    @Override
    public boolean operateAddRecord(String content, Long thirdId, Long userId, OperateTypeEnum type, String detail) {
        OperateRecordDo operateRecordDo = new OperateRecordDo();
        operateRecordDo.setContent(content);
        operateRecordDo.setOperateTime(new Date());
        operateRecordDo.setOperateBy(userId);
        operateRecordDo.setType(type.getValue());
        operateRecordDo.setThirdId(thirdId);
        operateRecordDo.setDeleteFlag(0);
        operateRecordDo.setRemarks(detail);
        dao.insert(operateRecordDo);
        return true;
    }


    /**
     * contentValue 多个以 !& 隔开
     *
     * @param content
     * @param contentValue
     * @param thirdId
     * @param userId
     * @param type
     * @return
     */
    @Override
    public boolean operateAddRecordAndValue(String content, String contentValue, Long thirdId, Long userId, String username, OperateTypeEnum type) {
        return this.operateAddRecordAndValue(content, contentValue, thirdId, userId, username, type, new Date());
    }

    //     * 1:CM订单 2:CM商品 3:CM库存 4:库存导入操作
    @Override
    public boolean operateAddRecordAndValue(String content, String contentValue, Long thirdId, Long userId, String username, OperateTypeEnum type, Date operateTime) {
        if (StringUtils.isBlank(content)) return true;
        OperateRecordDo operateRecordDo = new OperateRecordDo();
        operateRecordDo.setContent(content);
        operateRecordDo.setContentValue(contentValue);
        operateRecordDo.setOperateTime(operateTime);
        operateRecordDo.setOperateBy(userId);
        operateRecordDo.setOperateUsername(username);
        operateRecordDo.setType(type.getValue());
        operateRecordDo.setThirdId(thirdId);
        operateRecordDo.setDeleteFlag(0);
        dao.insert(operateRecordDo);
        return true;
    }


    @Override
    public void insertBatchMy(List<OperateRecordDo> list) {
        if (list.isEmpty()) {
            return;
        }
        dao.insertBatchMy(list);
    }
}
